#include "stdafx.h"
#include "InstanceTouch.h"
#include <sstream>
#include "GambleDef.h"
#include <time.h>
#include "CardDef.h"

DECL_INSTANCE_TOUCH(ETouch_GamebleDef);

struct  GamebleDefReflectionRegister
{
	GamebleDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_sDisplayName,std::string("DefaultDisplayName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_bIsActive,true).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iActiveMonth,201301).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iRunCost,30).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iSpecialCard,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes);
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iRefleshDropTable,INVALID_ID).Editor_SetAsRaw("fresh");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iSuperRefleshDropTable,INVALID_ID).Editor_SetAsRaw("fresh");	
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iRemove0FreeDrop,INVALID_ID).Editor_SetAsRaw("drop");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iRemove0Drop,INVALID_ID).Editor_SetAsRaw("drop");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iMaxLucky,100).Editor_SetAsRaw("lucky");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iPlace0LuckAdd,30).Editor_SetAsRaw("lucky");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iPlace1LuckAdd,20).Editor_SetAsRaw("lucky");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iPlace2LuckAdd,10).Editor_SetAsRaw("lucky");
		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iPlace3LuckAdd,5).Editor_SetAsRaw("lucky");

		REGISTER_REFLECTION_MEMBER(GamebleDef,m_iSuperFreshNeedRun,10).Editor_SetAsRaw();
		
		GlobalClassManager::Instance().RegisterClass(GamebleDef::s_GetReflection(),eDefaultDef_Gamble);
		GlobalDefManager::Instance().RegisterDefManger(&GambleDefManager::Instance());
	}
}___gamebledefreflectionregister;



rewrite  void GamebleDef::OnArchieveReaded()
{
	const CardDef * pDef=CardDefManager::Instance().LookupCardDefByLvl(m_iSpecialCard,0);
	if(pDef)
	{
		m_iSpecialCardDef=pDef->m_iID;
	}
	else
	{
		m_iSpecialCardDef=1;
	}
}

rewrite void GambleDefManager::OnArchieveReaded()
{
	m_pGamebleDef=NULL;
	__GambleDefManager::OnArchieveReaded();

	GetGambleDef();
}

const GamebleDef * GambleDefManager::GetGambleDef() 
{
	if(NULL!=m_pGamebleDef && (m_pGamebleDef->m_iActiveMonth == Util::YearMonth(::time(NULL))) )
	{
		return m_pGamebleDef;
	}
	else
	{
		m_pGamebleDef=NULL;
		std::vector<const GamebleDef *> vGambleDefs;
		Enumerate(vGambleDefs);
		for(uint32 ii=0;ii<vGambleDefs.size();ii++)
		{
			const GamebleDef * pDef=vGambleDefs[ii];
			if(pDef->m_bIsActive&& Util::YearMonth(::time(NULL))==pDef->m_iActiveMonth)
			{
				m_pGamebleDef=pDef;
				break;
			}
		}
		if(NULL==m_pGamebleDef)
		{
			printf("No Active GamebleDef!\n");
			m_pGamebleDef=&DefaultStaticItem();
		}
		return m_pGamebleDef;
	}

}